// This page serves as notify url in the payment process , we update thw database recors here resp.
// Created : jun 19,06
// Author : Rupinder Saini

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Data.SqlClient;
using System.Net;
using System.Text;
using System.IO;


public partial class Doctor_UpdateCheckout : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        string strFormValues = Request.Form.ToString();
        string strNewValue;
        string strResponse;
     //   string str = "";
        
        // Create the request back
        //HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr");
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.paypal.com/cgi-bin/webscr");

        // Set values for the request back
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        strNewValue = strFormValues + "&cmd=_notify-validate";
        req.ContentLength = strNewValue.Length;
        // Write the request back IPN strings
        StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
        stOut.Write(strNewValue);
        stOut.Close();

        // Do the request to PayPal and get the response
        StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
        strResponse = stIn.ReadToEnd();
        stIn.Close();
        Manager mgr = new Manager(true,"ConnectionString");

        try
        {
            mgr.ExecuteUpdate("insert into tbl_testdonotdelete values('" + "OUTTRx " + Request.Form["txn_id"] + "INV" + Request.Form["invoice"] + "invoi')");
        }
        catch(Exception ex)
        {
            mgr.sendMail(ex.ToString() + "OUTTRx " + Request.Form["txn_id"] + "INV" + Request.Form["invoice"] + "invoi')", "synapsetesting@gmail.com", "healthcare", "Payment Error", "info@healthcare.com", "sharvan@sampatti.com", "");
        }
       ////// // Confirm whether the IPN was VERIFIED or INVALID. If INVALID, just ignore the IPN        
       
        if (strResponse == "VERIFIED")
        {
            // Create the IpnTransaction
            
                    //check that the "payment_status" form field 
                  // if (String.Compare(Request.Form["payment_status"].ToString(), "Completed") == 0)
                   // {
                        // customer's payment has been recieved.
                        // now check if the amount has not been tampered with , duplicate trx_id if ok then update the trx status to paid

            ////mgr.ExecuteUpdate("insert into tbl_test values('[in]" + str.Replace("'", "''") + "')");
                   
                       // SqlParameter[] sqlParamupd = new SqlParameter[3];
                       // sqlParamupd[0] = new SqlParameter("@trx_id", SqlDbType.VarChar,250);
                       // sqlParamupd[1] = new SqlParameter("@bidid", SqlDbType.Int);
                       // sqlParamupd[2] = new SqlParameter("@amount", SqlDbType.Money);
                   
                       // sqlParamupd[0].Value= Request.Form["txn_id"];
                       // sqlParamupd[1].Value=Request.Form["invoice"];
                       // sqlParamupd[2].Value = Request.Form["payment_gross"];
                       //mgr.ExecuteUpdate("update_transactiondetails", sqlParamupd);
                 //  }

            /////////////////////////////////////////K - CODE START///////////////////////////////////////////////////
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////            
            try
            {
                SqlParameter[] sqlParamupd = new SqlParameter[4];
                sqlParamupd[0] = new SqlParameter("@trx_id", SqlDbType.VarChar, 250);
                sqlParamupd[1] = new SqlParameter("@bidid", SqlDbType.Int);
                //sqlParamupd[2] = new SqlParameter("@amount", SqlDbType.Money);
                sqlParamupd[2] = new SqlParameter("@subs_id", SqlDbType.VarChar, 250);
                sqlParamupd[3] = new SqlParameter("@txnType", SqlDbType.VarChar, 20);

                sqlParamupd[0].Value = Request.Form["txn_id"];
                sqlParamupd[1].Value = Convert.ToInt32(Request.Form["invoice"].ToString());
                //sqlParamupd[2].Value = "0.00"; // Request.Form["amount3"].ToString();
                sqlParamupd[2].Value = Request.Form["subscr_id"];
                sqlParamupd[3].Value = Request.Form["txn_type"];

                mgr.ExecuteUpdate("update_transactiondetails", sqlParamupd);                
            }
            catch (Exception ex)
            {
                mgr.sendMail(ex.ToString() + "OUTTRx " + Request.Form["txn_id"] + "INV" + Request.Form["invoice"] + "invoi')", "synapsetesting@gmail.com", "healthcare", "Payment Error 2", "info@healthcare.com", "sharvan@sampatti.com", "");
            }
            //mgr = null;
            //sqlParamupd = null;
            ///////////////////////////////////////////K CODE END//////////////////////////////////////////////////////
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////

                     
        }
        
        }
    }



